# 导入线程池
from multiprocessing.dummy import Pool
import requests
import time
# 定义一个方法，然后线程调用该方法
def cal_power2(num):
    return num * num

# 定义三个线程
pool = Pool(3)
# 定义一个列表
numList = [x for x in range(10)]

# 计算结果
result = pool.map(cal_power2,numList)

print(f'计算 0-9的平方为: {result}')



def query(url):
    contentResponse = requests.get(url)
    print(contentResponse.content.decode())
# 进行请示
"""
# 单线程: 36.328489542007446 
startTime = time.time();
for i in range(100):
    query("http://www.yueshushu.top")
endTime = time.time()
print(f'线程访问 100次首面，耗时 {endTime - startTime}')
"""

# 多线程:   5.152028560638428
startTime = time.time();
pool2 = Pool(10)
urlList = []
for i in range(100):
    urlList.append("http://www.yueshushu.top")
pool2.map(query,urlList)
endTime = time.time()
print(f'线程访问 100次首面，耗时 {endTime - startTime}')
